package com.example.jixeishi1.controller;


import com.example.jixeishi1.dao.AddressMapper;
import com.example.jixeishi1.dao.CartMapper;
import com.example.jixeishi1.dao.OrderShopMapper;
import com.example.jixeishi1.entity.AddressEntity;
import com.example.jixeishi1.entity.CartEntity;
import com.example.jixeishi1.entity.OrderEntity;
import com.example.jixeishi1.entity.OrderShopEntity;
import com.example.jixeishi1.mapper.OrderMapper;
import com.example.jixeishi1.service.OrderShopService;
import com.example.jixeishi1.utils.JsonResult;
import com.example.jixeishi1.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Controller
public class OrderShopController {
    @Autowired
    private OrderShopService orderShopService;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private OrderShopMapper orderShopMapper;
    @Autowired
    private OrderMapper orderMapper;
    /**
     * 列表
     */
    @RequestMapping("/ordershop/list")
    public ModelAndView orderShopList(){
        List<OrderShopEntity> orderShopEntities = new ArrayList<>();
        orderShopEntities = orderShopService.getorderShopAll();
        ModelAndView mav = new ModelAndView("paymentold.htmls");
        mav.addObject("ordershop" , orderShopEntities);
        return mav;
    }


    /**
     * 列表
     */
    @RequestMapping(value = "/ordershop/save/{id}")
    public ModelAndView orderShopsave(@PathVariable("id") Integer id){
        System.out.println(id+"......。。。。。。");
          orderShopService.saveordershop(id);

        OrderShopEntity orderShopEntity2 = orderShopMapper.getorderShopById(id);
        if(ObjectUtils.isEmpty(orderShopEntity2)){

        }else{

        }
        List<OrderShopEntity> orderShopEntities = orderShopMapper.getorderShopAll();
        Double all = 0.00;
        ModelAndView mav = new ModelAndView("redirect:/index");
        for(OrderShopEntity orderShopEntity : orderShopEntities){
            if(ObjectUtils.isEmpty(orderShopEntity)){

            }else{
                Integer num = orderShopEntity.getNum();
                Double money = orderShopEntity.getPricecommodity();
                all += num * money;
            }
        }
        if(all ==0.00){
            mav.addObject("all" , all);

        }else{
            Double allmoeny =  all-1.00;
            mav.addObject("all" , allmoeny);
        }
        return mav;
    }
    /**
     * 列表
     */
    @RequestMapping("/ordershop/del")
    public ModelAndView orderShopdel(@RequestParam("id") Integer id){
          orderShopService.delordershop(id);
        List<OrderShopEntity> orderShopEntities = orderShopMapper.getorderShopAll();
        Double all = 0.00;
        ModelAndView mav = new ModelAndView("redirect:/index");
        for(OrderShopEntity orderShopEntity : orderShopEntities){
            if(ObjectUtils.isEmpty(orderShopEntity)){

            }else{
                Integer num = orderShopEntity.getNum();
                Double money = orderShopEntity.getPricecommodity();
                all += num * money;
            }
        }
        if(all ==0.00){
            mav.addObject("all" , all);

        }else{
            Double allmoeny =  all-1.00;
            mav.addObject("all" , allmoeny);
        }
        return mav;
    }



    /**
     * 列表
     */
    @RequestMapping(value = "/ordershop/checkAll/{id}")
    public ModelAndView checkAll(@PathVariable("id") Integer id){
        orderShopService.checkAll(id);
        OrderShopEntity orderShopEntity2 = orderShopMapper.getorderShopById(id);
        if(ObjectUtils.isEmpty(orderShopEntity2)){

        }else{

        }
        List<OrderShopEntity> orderShopEntities = orderShopMapper.getorderShopAll();
        List<CartEntity> shoppingEntities = cartMapper.getShoppingInfo();
        Double all = 0.00;
        ModelAndView mav = new ModelAndView("redirect:/index");
        for(CartEntity cartEntity : shoppingEntities){
            if(ObjectUtils.isEmpty(cartEntity)){

            }else{
                Integer num = cartEntity.getNum();
                Double money = cartEntity.getPricecommodity();
                all += num * money;
            }
        }
        if(all ==0.00){
            mav.addObject("all" , all);

        }else{
            mav.addObject("all" , all);
        }
        OrderEntity order = new OrderEntity();
        order.setPrices(all);
        order.setRecevieWay(1);
        order.setMessage("1");
        order.setAddressId(1);
        order.setPayWay(1);
        orderMapper.saveOrder(order);
        return mav;
    }

    /**
     * 列表
     */
    @GetMapping(value = "/ordershop/deletebyid/{id}")
    public ModelAndView deleteordershop(@PathVariable("id") Integer id){
        orderShopService.delete(id);
        Double all = 0.00;
        List<OrderShopEntity> orderShopEntities = orderShopMapper.getorderShopAll();
        ModelAndView mav = new ModelAndView("redirect:/index");
        for(OrderShopEntity orderShopEntity : orderShopEntities){
            if(ObjectUtils.isEmpty(orderShopEntity)){

            }else{
                Integer num = orderShopEntity.getNum();
                Double money = orderShopEntity.getPricecommodity();
                all += num * money;
                System.out.println(num);
                System.out.println(money);
            }
        }
        if(all ==0.00){
            mav.addObject("all" , all);

        }else{
            Double allmoeny =  all-1.00;
            mav.addObject("all" , allmoeny);
        }
        return mav;
    }

    @GetMapping(value = "/ordershop/delete/{id}")
    public JsonResult deletecheckAll(@PathVariable("id") Integer id){
        orderShopService.delete(id);
        orderMapper.deleteOrder();
        JsonResult json = new JsonResult(true,"ajax查询成功",true);
        return json;
    }

    /**
     * 列表
     */
    @GetMapping("/payment")
    public ModelAndView orderShopdel(){
        List<OrderShopEntity> orderShopEntities = new ArrayList<>();
        orderShopEntities = orderShopService.getorderShopAll();
        List<CartVo> cartVos = new ArrayList<>();
        Integer i = 0;
        Integer index = 0;
        ModelAndView mav = new ModelAndView("payment");
        List<CartEntity> shoppingEntities = new ArrayList<>();
        List<AddressEntity> addressEntities = addressMapper.getListAddressByUid();
        mav.addObject("addresslist" , addressEntities);
        AddressEntity address = new AddressEntity();
        if(CollectionUtils.isEmpty(addressEntities)){

        }else{
            address  = addressEntities.get(0);
        }
        mav.addObject("address1" , address);
        Double all = 0.00;
        if(orderShopEntities.size()!=0 && orderShopEntities!=null){
            for(OrderShopEntity orderShopEntity:orderShopEntities){
                if(orderShopEntity.getId().equals(200)){
                    shoppingEntities = cartMapper.getShoppingInfo();
                    i = 1;
                }
            }
        }
        if(i.equals(1)){
            for(CartEntity cartEntity :shoppingEntities) {
                CartVo cartVo = new CartVo();
                Integer num = cartEntity.getNum();
                index +=num;
                Double oneAll = num * cartEntity.getPricecommodity();
                cartVo.setPricecommodityAll(oneAll);
                cartVo.setCmmodityName(cartEntity.getCmmodityName());
                cartVo.setPricecommodity(cartEntity.getPricecommodity());
                cartVo.setInTime(cartEntity.getInTime());
                cartVo.setNum(cartEntity.getNum());
                cartVo.setCommodityId(cartEntity.getCommodityId());
                cartVo.setMainImage(cartEntity.getMainImage());
                cartVos.add(cartVo);
                mav.addObject("ordershop", cartVos);
                all += oneAll;
            }
        }else if(i.equals(0)){
            for(OrderShopEntity orderShopEntity :orderShopEntities){
                CartVo cartVo = new CartVo();
                Integer num = orderShopEntity.getNum();
                index +=num;
                Double oneAll = num * orderShopEntity.getPricecommodity();
                cartVo.setPricecommodityAll(oneAll);
                cartVo.setCmmodityName(orderShopEntity.getCmmodityName());
                cartVo.setPricecommodity(orderShopEntity.getPricecommodity());
                cartVo.setInTime(orderShopEntity.getInTime());
                cartVo.setNum(orderShopEntity.getNum());
                cartVo.setCommodityId(orderShopEntity.getCommodityId());
                cartVo.setMainImage(orderShopEntity.getMainImage());
                cartVos.add(cartVo);
                all += oneAll;
            }
            mav.addObject("ordershop" , cartVos);
        }
        mav.addObject("allmoney" , all);
        mav.addObject("allindex" , index);

        return mav;
    }



}
